package cn.itrip.biz.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripHotelRoom;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.*;
import cn.itrip.biz.exception.AddOrderException;
import cn.itrip.biz.service.HotelOrderService;
import cn.itrip.biz.service.HotelRoomService;
import cn.itrip.biz.service.TradeEndsService;
import cn.itrip.utlis.DtoUtil;
import cn.itrip.utlis.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description :
 * Created by Li on 2020-12-28 10:39:32
 */
@Slf4j
@RestController
@RequestMapping("/api/hotelorder")
public class HotelOrderController {
    @Autowired
    private HotelOrderService hotelOrderService;
    @Autowired
    private HotelRoomService hotelRoomService;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private TradeEndsService tradeEndsService;
    @PostMapping("/getpersonalorderlist")
    public Dto getpersonalorderlist(@RequestBody ShowOrderVO showOrderVO, HttpServletRequest request){
        String token = request.getHeader("token");
        ItripUser user = tokenUtil.getUser(token);
        if(user == null) {
            return DtoUtil.returnFail("请先登录","100001");
        }
        //
        GetPersonalOrderListReturn listReturn = hotelOrderService.selectByVO(showOrderVO,user.getId());
        return DtoUtil.returnDataSuccess(listReturn);
    }


    @PostMapping("/addhotelorder")
    public Dto addhotelorder(@RequestBody ItripAddHotelOrderVO vo, HttpServletRequest request){
        String token = request.getHeader("token");
        ItripUser user = tokenUtil.getUser(token);
        if(user == null) {
            return DtoUtil.returnFail("请先登录","100001");
        }
        //
        ItripHotelOrder order = new ItripHotelOrder();
        BeanUtils.copyProperties(vo,order);
        order.setCreatedBy(user.getId());
        order.setCreationDate(vo.getCheckInDate());
        order.setModifyDate(vo.getCheckOutDate());
//        hotelOrderService.addhotelorder(order);
        try {
            order.setUserId(user.getId());
            order.setCreatedBy(user.getId());
            String linkUserName = vo.getLinkUser().stream().map(i->i.getLinkUserName()).collect(Collectors.joining(","));
            order.setLinkUserName(linkUserName);
            hotelOrderService.addhotelorder(order);
            order.setId(hotelOrderService.getMaxId());
        }catch (AddOrderException e) {
            log.error(e.getMessage(), e);
            return DtoUtil.returnFail(e.getMessage(), "30002");
        }
        Map<String, Object> date = new HashMap<>();
        date.put("orderNo", order.getOrderNo());
        date.put("id", order.getId());
        return DtoUtil.returnSuccess("生成订单成功", date);
    }

    @PostMapping("/getpreorderinfo")
    public Dto getpreorderinfo(@RequestBody GetPreorderInfoVO params){
        GetPreorderInfoVO roomStoreVO = new GetPreorderInfoVO();
        //设置酒店名称和房价
        ItripHotelRoom hotelRoom = hotelRoomService.selectById(params.getRoomId());
        roomStoreVO.setPrice(hotelRoom.getRoomPrice());
        roomStoreVO.setHotelName(hotelRoom.getHotelName());
        //设置最小库存数量
        List<StoreVO> storeVOList = hotelOrderService.getpreorderinfo(params);
        int count = params.getCount();
        int minCount = storeVOList.get(0).getStore();
        roomStoreVO.setStore(minCount);
        if(minCount < count){
                return DtoUtil.returnFail("库存不足","30003");
        }
        return DtoUtil.returnSuccess("获取成功",roomStoreVO);
    }

    @GetMapping("/queryOrderById/{id}")
    public Dto queryOrderById(@PathVariable Long id){
        ItripHotelOrder order = hotelOrderService.queryOrderById(id);
        return DtoUtil.returnDataSuccess(order);
    }

    @GetMapping("/getpersonalorderinfo/{id}")
    public Dto getpersonalorderinfo(@PathVariable Long id){
        ItripHotelOrder order = hotelOrderService.queryOrderById(id);
        return DtoUtil.returnDataSuccess(order);
    }

    @GetMapping("/getpersonalorderroominfo/{id}")
    public Dto getpersonalorderroominfo(@PathVariable Long id){
        ItripHotelOrder order = hotelOrderService.queryOrderById(id);
        return DtoUtil.returnDataSuccess(order);
    }

    @GetMapping("/scanTradeEnd")
    @ResponseBody
    public Dto scanTradeEnd() {
        tradeEndsService.tradeEnd();
        return DtoUtil.returnSuccess();
    }
}
